// (C) Copyright 2015 Moodle Pty Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { Component, ElementRef, OnDestroy, OnInit, inject, input } from '@angular/core';
import { CoreCancellablePromise } from '@classes/cancellable-promise';
import { CoreModals } from '@services/overlays/modals';
import { CoreDom } from '@singletons/dom';
import { ContextLevel } from '@/core/constants';
import { CoreSharedModule } from '@/core/shared.module';

/**
 * Component that displays a button to open blocks.
 */
@Component({
    selector: 'core-block-side-blocks-button',
    templateUrl: 'side-blocks-button.html',
    styleUrl: 'side-blocks-button.scss',
    imports: [
        CoreSharedModule,
    ],
})
export class CoreBlockSideBlocksButtonComponent implements OnInit, OnDestroy {

    readonly contextLevel = input.required<ContextLevel>();
    readonly instanceId = input.required<number>();
    readonly myDashboardPage = input<string>();

    protected element: HTMLElement = inject(ElementRef).nativeElement;
    protected slotPromise?: CoreCancellablePromise<void>;

    /**
     * @inheritdoc
     */
    async ngOnInit(): Promise<void> {
        this.slotPromise = CoreDom.slotOnContent(this.element);
    }

    /**
     * Open side blocks.
     */
    async openBlocks(): Promise<void> {
        const { CoreBlockSideBlocksComponent } = await import('@features/block/components/side-blocks/side-blocks');

        CoreModals.openSideModal({
            component: CoreBlockSideBlocksComponent,
            componentProps: {
                contextLevel: this.contextLevel(),
                instanceId: this.instanceId(),
                myDashboardPage: this.myDashboardPage(),
            },
        });
    }

    /**
     * @inheritdoc
     */
    ngOnDestroy(): void {
        this.slotPromise?.cancel();
    }

}
